// GPSPositionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GPSPosition.h"
#include "GPSPositionDlg.h"

#include <afxinet.h>
#include <sstream>
#include <winuser.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define LR_LOADFROMFILE = 0x0010

CGPSPositionDlg::CGPSPositionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGPSPositionDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pGps = NULL;
	m_pMap = NULL;
}

void CGPSPositionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_POS, m_liPos);
	DDX_Control(pDX, IDC_COMBO_POI, m_cbPoi);
	DDX_Control(pDX, IDC_STATIC_BITMAP, m_picMap);
}

BEGIN_MESSAGE_MAP(CGPSPositionDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CGPSPositionDlg::OnBnClickedButton1)
	ON_STN_CLICKED(IDC_STATIC_BITMAP, &CGPSPositionDlg::OnStnClickedStaticBitmap)
END_MESSAGE_MAP()


// CGPSPositionDlg message handlers

BOOL CGPSPositionDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_cbPoi.AddString(_T("Pizza"));
	m_cbPoi.AddString(_T("Movies"));
	m_cbPoi.AddString(_T("Parking"));
	m_cbPoi.AddString(_T("Restaurant"));
	m_cbPoi.SetCurSel(m_cbPoi.GetCount()-1);

	m_menu.LoadMenu(IDR_MENU_MAIN);
	SetMenu(&m_menu);

	m_pMap = new GoogleMap(CString("ABQIAAAAPP3Qmr9lPXZTqCVFP3cAaxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTi1YHAxeHT-4md6qaTGQKmbOtC_g"));
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CGPSPositionDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_GPSPOSITION_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_GPSPOSITION_DIALOG));
	}
}
#endif

void CGPSPositionDlg::OnBnClickedButton1()
{
	wchar_t * szText = new wchar_t[256];
	memset(szText, 0, sizeof szText);
	bool b = false;
	long len = 0;

	if (!m_pGps) {
		m_pGps = new Gps;
		b = m_pGps->openDevice();
	}

#if _DEBUG
	Gps::GPSCOORD * x = new Gps::GPSCOORD();
	x->latitude = 56.1616281;
	x->longitude = 10.2102788;
#else
	Gps::GPSCOORD * x = m_pGps->getPosition();
#endif

	ListPosition(x);
	if (0 < x->longitude && 0 < x->latitude) {
		m_pMap->addMarker(x->latitude, x->longitude);
		
		CString keyword = getKeyword();

		CString s = m_pMap->getKeywordUrl(x->latitude, x->longitude, keyword);
		BYTE * dataPtr = m_pMap->loadHttpFile(s, len);

		std::vector<CString> addrs = m_pMap->getAddressesFromKey(keyword);
		std::vector<CString>::iterator ite;
		for (ite = addrs.begin(); ite != addrs.end(); ++ite) {
			double lat = 0.0, lng = 0.0;
			CString s = *ite;
			m_pMap->getCoordByAddr(s, lat, lng);
			m_pMap->addMarker(lat, lng);
		}

		CString url = m_pMap->getMapUrl(210, 180, x->longitude, x->latitude, 16, CString("JPEG"));
		BYTE * data = m_pMap->loadHttpFile(url, len);

		CString picurl = m_pMap->makeImgFile(data, len);
		showPicture(picurl);
	}
	delete x;

	UpdateData(0);
}


void CGPSPositionDlg::ListPosition(Gps::GPSCOORD * coord)
{
	long len = 0;
	wchar_t * szText = new wchar_t[256];
	memset(szText, 0, sizeof szText);

	PCWSTR pszFormat=L"Lat: %f, Long: %f\0";
	wsprintf(szText,pszFormat,
			 coord->latitude,
			 coord->longitude);
	// m_liPos.AddString(szText);

	CString descurl = m_pMap->getDescriptivLocationUrl(coord->latitude, coord->longitude);
	BYTE* x = m_pMap->loadHttpFile(descurl, len);
	m_liPos.AddString((CString)x);

	m_liPos.SetCaretIndex(m_liPos.GetCount() -1);
}

void CGPSPositionDlg::showPicture(CString filepath)
{
	void * p = SHLoadImageFile(filepath);
	if (p) {
		HBITMAP hBitmap = static_cast <HBITMAP> (p);
		CBitmap *q = CBitmap::FromHandle(hBitmap);
		q->SetBitmapDimension(15,15);
		m_picMap.SetBitmap(*q);
	}
}

CString CGPSPositionDlg::getKeyword()
{
	CString str;
	m_cbPoi.GetLBText(m_cbPoi.GetCurSel(), str);
	return str;
}

void CGPSPositionDlg::OnStnClickedStaticBitmap()
{
	// TODO: Add your control notification handler code here
}
